define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'project/index',
                    add_url: 'project/add',
                    edit_url: 'project/edit',
                    del_url: 'project/del',
                    multi_url: 'project/multi',
                    import_url: 'project/import',
                    check_url: 'project/check',
                    dragsort_url: '',
                    table: 'category',
                },
            });

            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                escape: false,
                pk: 'id',
                commonSearch: false,
                search: true,
                columns: [
                    [
                         {checkbox: true},
                         {field: 'id', title: __('Id')},
                         {field:'number', title: __('number')},
                         {field:'ministries.m_name', title: __('mname')},
                         {field:'cate.name', title: __('Catename')},
                         {field:'title', title: __('title')},
                         {field:'subject.subject', title: __('subject')},
                         {field: 'ctime', title: __('ctime'), operate:'RANGE', formatter: Table.api.formatter.datetime, data: 'data-date-format="MM-DD"'},
                         {field: 'operate', title: __('Operate'), table: table,
                             events: Table.api.events.operate, formatter: Table.api.formatter.operate},

                    ]
                ]
            };


            // 初始化表格
            table.bootstrapTable(tableOptions);

            // 为表格绑定事件
            Table.api.bindevent(table);

            //修改添加是弹窗大小
            $(".btn-add").data("area", ["80%","80%"]); //自定义弹窗大小
            //修改编辑弹窗大小
            table.on('post-body.bs.table',function () {
                $(".btn-editone").data("area", ["80%","80%"]); //自定义弹窗大小
            });

            //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#', '');
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    // params.filter = JSON.stringify({type: typeStr});
                    params.type = typeStr;

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });



            //初始化 基础数据导入 不可操作
         $("#errorhtml").click(function () {
             //存在异常
             Fast.api.open("projecterror/index", "异常数据统计", {
                 'area':[
                     $(window).width() > 800 ? '80%' : '800',
                     $(window).height() > 600 ? '80%' : '600'
                 ],
             });
         });


            //必须默认触发shown.bs.tab事件
            // $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");

        },
        add: function () {

            Controller.api.bindevent();
            setTimeout(function () {
                $("#c-type").trigger("change");
            }, 100);
            //项目区域
            $(document).on("change", "#area", function () {
                var val = $(this).val()

                if(val === ""){
                    $("#areacodediv").css("display","none");
                }else{
                    $("#areacodediv").css("display","block");

                    $("#areacode option[pid"+ val +" ='"+ val +"']").css("display","block");
                    $("#areacode option[pid"+ val +" !='"+ val +"']").css("display","none");

                }
                //动态刷新 select option下数据
                $("#areacode").selectpicker('refresh', {});


            });

            //项目板块
            $(document).on("change", "#unit_id", function () {
                var val = $(this).val()
                if(val === ""){
                    $("#Catename").css("display","none");
                    $("#a_id option[pid ='"+ 0 +"']").prop("selected", true);
                }else{
                    $("#Catename").css("display","block");
                    $("#a_id option[pid ='"+ val +"']").css("display","block");
                    $("#a_id option[pid !='"+ val +"']").css("display","none");

                }
                //动态刷新 select option下数据
                $("#a_id").selectpicker('refresh', {});


            });



        },
        edit: function () {
            Controller.api.bindevent();

            //区域代码


            var codepid = $("#areacode").attr("codepid");
            $("#areacode option[value ='"+ codepid +"']").prop("selected", true);





            //项目板块
            var pid = $("#a_id").attr("pid");
            $("#a_id option").css("display","none");
            $("#a_id option[pid ='"+ pid +"']").css("display","block");

            $(document).on("change", "#unit_id", function () {
                var val = $(this).val()
                if(val === ""){
                    $("#Catename").css("display","none");
                    $("#a_id option[pid ='"+ 0 +"']").prop("selected", true);
                }else{
                    $("#Catename").css("display","block");
                    $("#a_id option[pid ='"+ val +"']").css("display","block");
                    $("#a_id option[pid !='"+ val +"']").css("display","none");

                }
                //动态刷新 select option下数据
                $("#a_id").selectpicker('refresh', {});

            });

            //选中资金来源
            var hpaysytleval = $("#payment").val();

            var hpaysytlearr= new Array(); //定义一数组
            hpaysytlearr = hpaysytleval.split(",");
            for (i=0;i<hpaysytlearr.length ;i++ )
            {
                $("#paymentsel input[value="+ hpaysytlearr[i] +"]").attr("checked","checked");

            }



        },
        api: {
            bindevent: function () {
                //建设单位
              /*  var bid = $("#p_bid").attr("ids");
                $("#p_bid ")
                $("#p_bid option[data-type='"+ bid +"']").prop("selected", true);
                //项目等级
                var lid = $("#p_lid").attr("ids");
                $("#p_lid ")
                $("#p_lid option[data-type='"+ lid +"']").prop("selected", true);
                //当前阶段
                var sid = $("#p_sid").attr("ids");
                $("#p_sid ")
                $("#p_sid option[data-type='"+ sid +"']").prop("selected", true);
                //所属板块
                var cid = $("#p_cid").attr("ids");
                $("#p_cid ")
                $("#p_cid option[data-type='"+ cid +"']").prop("selected", true);
*/

                $(document).on("change", "#c-type", function () {

                    $("#c-pid option[data-type='all']").prop("selected", true);
                    $("#c-pid option").removeClass("hide");
                    $("#c-pid option[data-type!='" + $(this).val() + "'][data-type!='all']").addClass("hide");
                    $("#c-pid").data("selectpicker") && $("#c-pid").selectpicker("refresh");
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
